package com.chwn.curiousdiary.adapter;

import android.content.Context;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.MessageBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.base.MultiBaseAdapter;
import com.chwn.harulib.util.TimeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.chwn.curiousdiary.R.id.tv_read_state;

/**
 * Created by Myshikin on 2017/3/24.
 */
public class MessageAdapter extends MultiBaseAdapter<MessageBean> {

    private List<MessageBean> dirtyDatas;

    public MessageAdapter(Context context, List<MessageBean> datas, boolean isOpenLoadMore) {
        super(context, datas, isOpenLoadMore);
    }

    @Override
    protected void convert(ViewHolder holder, MessageBean data, int viewType) {


        if (viewType == Constant.MESSAGE_TYPE) {// 消息详情
            holder.setText(R.id.tv_message_content, data.getMessageContent());
            if (data.getReadflag() == Constant.MESSAGE_READ) {
                holder.getView(tv_read_state).setBackgroundResource(R.drawable.shape_message_read_state);
                holder.setText(tv_read_state , "已读");

            } else {
                holder.getView(tv_read_state).setBackgroundResource(R.drawable.shape_message_unread_state);
                holder.setText(tv_read_state , "未读");
            }

        } else if (viewType == Constant.DATA_TYPE) {//消息日期
            holder.setText(R.id.tv_message_date, "" + data.getDate());
        }
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        if (viewType == Constant.MESSAGE_TYPE) {
            return R.layout.item_message_info;
        } else {
            return R.layout.item_message_time;
        }
    }

    @Override
    protected int getViewType(int position, MessageBean data) {
        if (data.isDate()) {
            return Constant.DATA_TYPE;
        }
        return Constant.MESSAGE_TYPE;
    }


    @Override
    public void setNewData(List<MessageBean> datas) {
        dirtyDatas = datas ;
        sortData(datas);

        List<MessageBean> newMessageList = new ArrayList<>();
        newMessageList.add(new MessageBean(true, datas.get(0).getDate()));
        for (int i = 0; i < datas.size(); i++) {
            if (!TimeUtils.isOneDay(newMessageList.get(newMessageList.size() - 1).getDate(), datas.get(i).getDate())) {
                newMessageList.add(new MessageBean(true, datas.get(i).getDate()));
            }
            newMessageList.add(datas.get(i));
        }
        if (mDatas != null) {
            mDatas.clear();
            mDatas.addAll(newMessageList);
        }
        notifyDataSetChanged();
    }

    private void  sortData(List<MessageBean> datas) {
        Collections.sort(datas, new Comparator<MessageBean>() {
            @Override
            public int compare(MessageBean messageBean, MessageBean t1) {
                return TimeUtils.isOldTime(messageBean.getDate(), t1.getDate());
            }
        });
    }

    @Override
    public void setLoadMoreData(List<MessageBean> datas) {
        dirtyDatas.addAll(datas);
        setNewData(dirtyDatas);
       /* List<MessageBean> newMessageList = new ArrayList<>();
        int i;
        for (i = 0; i < datas.size(); i++) {
            if (mDatas.get(mDatas.size() - 1).getMessageTime().equals(datas.get(i).getMessageTime())) {
                newMessageList.add(datas.get(i));
            }
        }

        for (int j = i; j < datas.size(); j++) {
            if (!TimeUtils.isOneDay(newMessageList.get(newMessageList.size() - 1).getDate(), datas.get(j).getDate())) {
                newMessageList.add(new MessageBean(true, datas.get(j).getDate()));
            }
            newMessageList.add(datas.get(j));
        }
        mDatas.addAll(mDatas.size(), newMessageList);*/
    }
}
